package com.thz.admin.controller.pdf;

import com.thz.common.dto.pdf.ConversionDocxToPdfRequestDTO;
import com.thz.common.dto.pdf.ConversionDocxToPdfResponseDTO;
import com.thz.common.result.Result;
import com.thz.common.utils.IpUtils;
import com.thz.system.service.pdf.IConversionDocxToPdfService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * DOCX 转 PDF 控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/conversion/docxToPdf")
@Tag(name = "DOCX转PDF", description = "使用LibreOffice将DOCX转换为PDF")
public class DocxToPdfController {

    @Autowired
    private IConversionDocxToPdfService conversionDocxToPdfService;

    @PostMapping("/upload")
    @Operation(summary = "上传并转换DOCX到PDF", description = "基于LibreOffice的高质量DOCX转PDF能力")
    public Result<ConversionDocxToPdfResponseDTO> uploadAndConvert(@RequestParam("file") MultipartFile file,
                                                                   @RequestParam(value = "pdfa", required = false) Boolean pdfa,
                                                                   @RequestParam(value = "embedStandardFonts", required = false) Boolean embedStandardFonts,
                                                                   @RequestParam(value = "exportBookmarks", required = false) Boolean exportBookmarks,
                                                                   HttpServletRequest request) {
        if (file == null || file.isEmpty()) {
            return Result.error("上传文件不能为空");
        }
        ConversionDocxToPdfRequestDTO req = new ConversionDocxToPdfRequestDTO();
        if (pdfa != null) {
            req.setPdfa(pdfa);
        }
        if (embedStandardFonts != null) {
            req.setEmbedStandardFonts(embedStandardFonts);
        }
        if (exportBookmarks != null) {
            req.setExportBookmarks(exportBookmarks);
        }
        String ip = IpUtils.getClientIp(request);
        ConversionDocxToPdfResponseDTO responseDTO = conversionDocxToPdfService.convertDocxToPdf(file, req, ip);
        return Result.success("转换成功", responseDTO);
    }

    @GetMapping("/{id:\\d+}")
    @Operation(summary = "查询转换记录", description = "根据ID查询DOCX转PDF记录")
    public Result<ConversionDocxToPdfResponseDTO> getRecord(@PathVariable Long id) {
        return Result.success(conversionDocxToPdfService.getConversionRecord(id));
    }

    @GetMapping("/recent")
    @Operation(summary = "查询最近的转换记录", description = "获取最近的DOCX转PDF记录")
    public Result<List<ConversionDocxToPdfResponseDTO>> getRecent(@RequestParam(value = "limit", defaultValue = "10") int limit) {
        return Result.success(conversionDocxToPdfService.getRecentConversions(limit));
    }

    @DeleteMapping("/{id:\\d+}")
    @Operation(summary = "删除转换记录", description = "删除指定的DOCX转PDF记录及文件")
    public Result<Void> deleteRecord(@PathVariable Long id) {
        conversionDocxToPdfService.deleteConversion(id);
        return Result.success("删除成功", null);
    }
}

